package model

import (
	"gorm.io/gorm"
)

type Problem struct {
	gorm.Model
	Identity          string             `gorm:"column:identity;type:varchar(36);" json:"identity"`
	ProblemCategories []*ProblemCategory `gorm:"foreignKey:problem_id;references:id"`
	Title             string             `gorm:"column:title;type:varchar(255);" json:"title"`
	Content           string             `gorm:"column:content;type:text;" json:"content"`
	TotalNum          int                `gorm:"column:total_num;type:int;" json:"total_num"`
	MaxRuntime        int                `gorm:"column:max_runtime;type:int;" json:"max_runtime"`
	MaxMemory         int                `gorm:"column:max_memory;type:int;" json:"max_memory"`
	TestCases         []*TestCase        `gorm:"foreignKey:problem_id;references:id"`
	PassNum           int                `gorm:"column:pass_num;type:int;" json:"pass_num"`
	SubmitNum         int                `gorm:"column:submit_num;type:int;" json:"submit_num"`
}

func (table *Problem) TableName() string {
	return "problem"
}

// 处理数据逻辑
// 获取题目列表
func GetProblemList(keyword string, category_id string) *gorm.DB {
	//select * from problem
	//left join problem_category on problem_category.problem_id = problem.id
	//left join category on category.id = problem_category.category_id
	//where category_id = ?
	tx := DB.Model(new(Problem)).Preload("ProblemCategories").Preload("ProblemCategories.Category").
		Where("title LIKE ? or content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
	if category_id != "" {
		tx = tx.Joins("left join problem_category on problem_category.problem_id = problem.id").
			Where("problem_category.category_id = ?", category_id)
	}
	return tx
}
